package ru.ifmo.itmokis.algo;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import ru.ifmo.itmokis.domain.Material;
import ru.ifmo.itmokis.domain.OrderedProduct;

/**
 *
 * @author Ivan Stepuk
 */
public class Utils {

    public static boolean detectChangeover(OrderedProduct fst, OrderedProduct snd) {
        if (fst.getProduct().equals(snd.getProduct())) {
            return false;
        }
        return true;
    }

    public static boolean detectFailedOrder(OrderedProduct op) {
        if (op.getOrder().getDeadline() == null) {
            return false;
        }
        if (op.getFinishTime().compareTo(op.getOrder().getDeadline()) > 0) {
            return true;
        }
        return false;
    }

    public static Map<Material, Integer> sumRecipes(Map<Material, Integer> fst, Map<Material, Integer> snd) {
        Map<Material, Integer> result = new HashMap<Material, Integer>();
        Set<Material> union = new HashSet<Material>();
        union.addAll(fst.keySet());
        union.addAll(snd.keySet());

        for (Material m : union) {
            int a = fst.get(m) != null ? fst.get(m) : 0;
            int b = snd.get(m) != null ? snd.get(m) : 0;
            result.put(m, a + b);
        }
        return result;
    }
}
